import api from '../../api/index'
import util from '../util'

export default class JsonStorage {
    constructor(name, mainKeyName) {
        if (!name) {
            throw new Error('缺少储存key')
        }
        if (!mainKeyName) {
            throw new Error('缺少主键key')
        }
        this.name = name
        this.mainKeyName = mainKeyName
    }

    async find() {
        try {
            const [res] = await api.post('/api/user/storage/get', {
                data: {
                    key: this.name
                }
            })
            if (util.isJson(res.value)) {
                return JSON.parse(res.value)
            } else {
                return null
            }
        } catch (e) {
            return Promise.reject(e)
        }
    }
    async find2() {
        try {
            var res = await api.post('/api/mirror/thirdequbind/queryListByUserid', {
                data: {
                    mode: "",
                    type: "aiaole,ailink"
                }
            })
            if (res) {
                return res
            } else {
                return null
            }
        } catch (e) {
            return Promise.reject(e)
        }
    }

    async detail(id) {
        let mainKeyName = this.mainKeyName
        console.log(this.mainKeyName);
        try {
            const list = await this.find()
            if (!list || list.length === 0 || !mainKeyName) {
                return null
            }
            let index = -1
            list.forEach((item, i) => {
                list[i][mainKeyName] = false
                if (item[mainKeyName] === data[mainKeyName]) {
                    index = i
                }
            })
            if (index > -1) {
                return list[index]
            }
            return null
        } catch (e) {
            return Promise.reject(e)
        }
    }

    // mainKeyName 唯一主键
    async save(data) {
        console.log(this.mainKeyName);
        let mainKeyName = this.mainKeyName
        try {
            const list = await this.find()
            console.log(list);
            if (!list || list.length === 0 || !this.mainKeyName) {
                return this.saveData([data])
            }

            let index = -1
            list.forEach((item, i) => {
                if (item[mainKeyName] === data[mainKeyName]) {
                    index = i
                }
            })
            if (index > -1) {
                list[index] = data
            }
            return this.saveData(list)
        } catch (e) {
            console.log('保存失败')
            return Promise.reject(e)
        }
    }

    async setDefault(id, defaultKeyName = 'defaultDevice') {
        try {
            const list = await this.find()
            console.log(list)
            console.log(id)
            console.log(defaultKeyName)
            if (!list || list.length === 0 || !defaultKeyName) {
                throw new Error('list为空或者主键不正确')
            }
            let index = -1
            list.forEach((item, i) => {
                list[i][defaultKeyName] = false
                if (item[this.mainKeyName] === id) {
                    index = i
                }
            })
            console.log(index)
            if (index > -1) {
                list[index][defaultKeyName] = true
            } else {
                throw new Error('未匹配到对应主键')
            }
            return this.saveData(list)
        } catch (e) {
            console.log('添加默认设备失败')
            return Promise.reject(e)
        }
    }

    async getDefault(defaultKeyName = 'defaultDevice') {
        try {
            const list = await this.find()
            console.log(list)
            if (!list || list.length === 0) {
                return Promise.reject({
                    code: 'list_none'
                })
            }
            const res = list.filter(item => {
                return item[defaultKeyName]
            })
            if (res.length > 0) {
                return res[0]
            } else {
                return Promise.reject({
                    code: 'default_none'
                })
            }
        } catch (e) {
            console.log('获取默认设备失败', e)
            return Promise.reject(e)
        }
    }

    async remove(id) {
        try {
            const list = await this.find()
            if (!list || list.length === 0) {
                throw new Error('list为空或者主键不正确')
            }
            let index = -1
            list.forEach((item, i) => {
                if (item[this.mainKeyName] === id) {
                    index = i
                }
            })
            console.log(index)
            if (index > -1) {
                list.splice(index, 1)
            } else {
                throw new Error('未匹配到对应主键')
            }
            return this.saveData(list)
        } catch (e) {
            console.log('删除失败')
            return Promise.reject(e)
        }
    }

    clear() {
        return api.post('/api/user/storage/set', {
            data: {
                key: this.name,
                value: '[]'
            }
        })
    }

    saveData(data) {
        console.log(this.name);
        console.log(data);
        return api.post('/api/user/storage/set', {
            data: {
                key: this.name,
                value: JSON.stringify(data)
            }
        })
    }
}
